package cn.buaa.anniversary.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

@Data
public class Word implements Serializable {

    private List<String> words;

    @JsonIgnore
    public Sentence getOne(){

        List<List<Sentence>> pre = new ArrayList<>();

        for(String word : words){
            pre.add(Dic.getMatches(word));
        }

        List<Sentence> flag = pre.get(0);
        for (int i = 1; i < pre.size(); i++) {
            flag = getJoin(flag, pre.get(i));
        }

        Random random = new Random();

        int index = random.nextInt(flag.size());
        return flag.get(index);
    }

    @JsonIgnore
    private List<Sentence> getJoin(List<Sentence> aList, List<Sentence> bList){
        List<Sentence> joinList = new ArrayList<>();
        for (Sentence anAList : aList) {
            for (Sentence aBList : bList) {
                if (Objects.equals(anAList.getId(), aBList.getId())) {
                    joinList.add(anAList);
                    break;
                }
            }
        }
        return joinList;
    }
}
